home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtoybm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  2.5 KB  |  116 lines

  1. /* pbmtoybm.c - read a pbm and write a file for Bennet Yee's 'xbm' and 'face'
  2. ** programs.
  3. **
  4. ** Written by Jamie Zawinski based on code (C) 1988 by Jef Poskanzer.
  5. **
  6. ** Permission to use, copy, modify, and distribute this software and its
  7. ** documentation for any purpose and without fee is hereby granted, provided
  8. ** that the above copyright notice appear in all copies and that both that
  9. ** copyright notice and this permission notice appear in supporting
  10. ** documentation.  This software is provided "as is" without express or
  11. ** implied warranty.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include "pbm.h"
  16.  
  17. #define YBM_MAGIC  ( ( '!' << 8 ) | '!' )
  18.  
  19. static void putinit ARGS(( int cols, int rows ));
  20. static void putbit ARGS(( bit b ));
  21. static void putrest ARGS(( void ));
  22. static void putitem ARGS(( void ));
  23.  
  24. void
  25. main( argc, argv )
  26.     int argc;
  27.     char* argv[];
  28.     {
  29.     FILE* ifp;
  30.     bit* bitrow;
  31.     register bit* bP;
  32.     int rows, cols, format, padright, row, col;
  33.  
  34.     pbm_init( &argc, argv );
  35.  
  36.     if ( argc > 2 )
  37.     pm_usage( "[pbmfile]" );
  38.     if ( argc == 2 )
  39.     ifp = pm_openr( argv[1] );
  40.     else
  41.     ifp = stdin;
  42.  
  43.     pbm_readpbminit( ifp, &cols, &rows, &format );
  44.     bitrow = pbm_allocrow( cols );
  45.     
  46.     /* Compute padding to round cols up to the nearest multiple of 16. */
  47.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  48.  
  49.     putinit( cols, rows );
  50.     for ( row = 0; row < rows; ++row )
  51.     {
  52.     pbm_readpbmrow( ifp, bitrow, cols, format );
  53.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  54.         putbit( *bP );
  55.     for ( col = 0; col < padright; ++col )
  56.         putbit( 0 );
  57.         }
  58.  
  59.     if ( ifp != stdin )
  60.     fclose( ifp );
  61.  
  62.     putrest( );
  63.  
  64.     pm_close (stdout);
  65.  
  66.     exit( 0 );
  67.     }
  68.  
  69. static long item;
  70. static int bitsperitem, bitshift;
  71.  
  72. static void
  73. putinit( cols, rows )
  74.     int cols, rows;
  75.     {
  76.     pm_writebigshort( stdout, YBM_MAGIC );
  77.     pm_writebigshort( stdout, cols );
  78.     pm_writebigshort( stdout, rows );
  79.     item = 0;
  80.     bitsperitem = 0;
  81.     bitshift = 0;
  82.     }
  83.  
  84. #if __STDC__
  85. static void
  86. putbit( bit b )
  87. #else /*__STDC__*/
  88. static void
  89. putbit( b )
  90.     bit b;
  91. #endif /*__STDC__*/
  92.     {
  93.     if ( bitsperitem == 16 )
  94.     putitem( );
  95.     ++bitsperitem;
  96.     if ( b == PBM_BLACK )
  97.     item += 1 << bitshift;
  98.     ++bitshift;
  99.     }
  100.  
  101. static void
  102. putrest( )
  103.     {
  104.     if ( bitsperitem > 0 )
  105.     putitem( );
  106.     }
  107.  
  108. static void
  109. putitem( )
  110.     {
  111.     pm_writebigshort( stdout, item );
  112.     item = 0;
  113.     bitsperitem = 0;
  114.     bitshift = 0;
  115.     }
  116.